package org.example.infrastructure.persistent.repository;

import com.alibaba.fastjson.JSON;
import org.example.domain.activity_manage.model.entity.ActivityStrategyPrizeEntity;
import org.example.domain.activity_manage.repository.IActivityStrategyPrizeRepo;
import org.example.infrastructure.persistent.dao.IActivityStrategyPrizeDao;
import org.example.infrastructure.persistent.po.ActivityStrategyPrizePO;
import org.example.types.common.RedisKeysPrefix;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lxc18
 * @date 2025/3/23 17:12
 * @description ActivityStrategyPrizeRepo
 */
@Repository
public class ActivityStrategyPrizeRepo implements IActivityStrategyPrizeRepo {
    @Resource
    private IActivityStrategyPrizeDao activityStrategyPrizeDao;
    @Resource
    private RedissonClient redissonClient;
    @Override
    public boolean updateStrategy(ActivityStrategyPrizeEntity activityStrategyPrize) {
        //先修改缓存，再修改数据库
        String cacheKey = RedisKeysPrefix.ACTIVITY_STRATEGY + activityStrategyPrize.getActivityId();
        //hash存储 key:activityId 字段 strategyName  值：ActivityStrategyPrizeEntity
        RMap<String, String> map = redissonClient.getMap(cacheKey);
        map.put(activityStrategyPrize.getStrategyName(), JSON.toJSONString(activityStrategyPrize));

        //修改数据库
        ActivityStrategyPrizePO po = ActivityStrategyPrizePO.builder()
                .activityId(activityStrategyPrize.getActivityId())
                .strategyName(activityStrategyPrize.getStrategyName())
                .prizeIds(activityStrategyPrize.getPrizeIds())
                .config(activityStrategyPrize.getConfig())
                .build();
        int updated = activityStrategyPrizeDao.updateStrategyConfig(po);
        if(updated > 0){
            return true;
        }
        return false;
    }

    @Override
    public ActivityStrategyPrizeEntity queryStrategy(Long activityId, String strategyName) {
        //先查缓存，再查数据库
        String cacheKey = RedisKeysPrefix.ACTIVITY_STRATEGY + activityId;
        RMap<String, String> map = redissonClient.getMap(cacheKey);
        String jsonStr = map.get(strategyName);
        ActivityStrategyPrizeEntity activityStrategyPrizeEntity = JSON.parseObject(jsonStr, ActivityStrategyPrizeEntity.class);;
        if(activityStrategyPrizeEntity != null){
            return activityStrategyPrizeEntity;
        }
        //查数据库
        ActivityStrategyPrizePO activityStrategyPrizePO = activityStrategyPrizeDao.queryStrategy(activityId, strategyName);
        activityStrategyPrizeEntity = new ActivityStrategyPrizeEntity();
        activityStrategyPrizeEntity.setActivityId(activityStrategyPrizePO.getActivityId());
        activityStrategyPrizeEntity.setStrategyName(activityStrategyPrizePO.getStrategyName());
        activityStrategyPrizeEntity.setPrizeIds(activityStrategyPrizePO.getPrizeIds());
        activityStrategyPrizeEntity.setConfig(activityStrategyPrizePO.getConfig());
        //存入缓存
        map.put(strategyName, JSON.toJSONString(activityStrategyPrizeEntity));
        return activityStrategyPrizeEntity;
    }

    @Override
    public List<ActivityStrategyPrizeEntity> queryStrategyByActivityId(Long activityId) {
        //先查缓存，再查数据库
        String cacheKey = RedisKeysPrefix.ACTIVITY_STRATEGY + activityId;
        RMap<String, String> map = redissonClient.getMap(cacheKey);
        List<ActivityStrategyPrizeEntity> activityStrategyPrizeEntities = new ArrayList<>();
        for(Map.Entry<String, String> entry : map.entrySet()){
            ActivityStrategyPrizeEntity activityStrategyPrizeEntity = JSON.parseObject(entry.getValue(), ActivityStrategyPrizeEntity.class);
            activityStrategyPrizeEntities.add(activityStrategyPrizeEntity);
        }
        if(activityStrategyPrizeEntities.size() > 0){
            return activityStrategyPrizeEntities;
        }
        //查数据库
        List<ActivityStrategyPrizePO> activityStrategyPrizePOS = activityStrategyPrizeDao.queryStrategyByActivityId(activityId);
        for(ActivityStrategyPrizePO activityStrategyPrizePO : activityStrategyPrizePOS){
            ActivityStrategyPrizeEntity activityStrategyPrizeEntity = ActivityStrategyPrizeEntity.builder()
                        .activityId(activityStrategyPrizePO.getActivityId())
                        .strategyName(activityStrategyPrizePO.getStrategyName())
                        .prizeIds(activityStrategyPrizePO.getPrizeIds())
                        .config(activityStrategyPrizePO.getConfig())
                        .build();
            activityStrategyPrizeEntities.add(activityStrategyPrizeEntity);
            //存入缓存
            map.put(activityStrategyPrizePO.getStrategyName(), JSON.toJSONString(activityStrategyPrizeEntity));

        }
        return activityStrategyPrizeEntities;
    }

    @Override
    public boolean createStrategy(Long activityId, String strategyName) {
        return activityStrategyPrizeDao.createStrategy(activityId, strategyName);
    }
}
